#ifndef ROTATE_CUBE_H_
#define ROTATE_CUBE_H_

#include <vector>

#include <SFML/Graphics.hpp>

#include "igame.hpp"

class RotateCube: public lge2d::IGame {
private:
	std::vector<float> cube_mesh_positions_array;
	std::vector<float> cube_mesh_normals_array;
	std::vector<short> cube_mesh_pos_and_norm_index_array;
	std::vector<short> cube_mesh_indexes;

	float theta[3];
	int axis;
public:
	RotateCube() {
		theta[0] = 0;
		theta[1] = 0;
		theta[2] = 0;

		axis = 0;
	}

	virtual void Initialize() {
		theta[0] = 0;
		theta[1] = 0;
		theta[2] = 0;

		float array[] = { 1, 1, -1, 1, -1, -1, -1, -0.9999998, -1, -0.9999997,
				1, -1, 1, 0.9999995, 1, 0.9999993, -1.000001, 1, -1, -0.9999997,
				1, -1, 1, 1 };
		//{-1.5f, -1.f, 0.f, 0.5f, 0.9999994f, 0.f, 0.5f, -1.000001f, 0.f};
		const int count = sizeof(array) / sizeof(float);
		cube_mesh_positions_array.reserve(count);
		for (int i = 0; i < count; ++i) {
			float value = array[i];
			cube_mesh_positions_array.push_back(value);
		}

		float normals_array[] = { 0, 0, -1 };
		const int normals_count = sizeof(normals_array) / sizeof(float);
		cube_mesh_normals_array.reserve(normals_count);
		for (int i = 0; i < normals_count; ++i) {
			float value = normals_array[i];
			cube_mesh_normals_array.push_back(value);
		}

		short vertex_and_normal_index_array[] = //{2, 0, 0, 0, 1, 0};
				{ 7, 0, 0, 0, 3, 0, 4, 1, 0, 1, 7, 1, 2, 2, 6, 2, 7, 2, 2, 3, 7,
						3, 3, 3, 1, 4, 5, 4, 2, 4, 5, 5, 6, 5, 2, 5, 0, 6, 4, 6,
						1, 6, 4, 7, 5, 7, 1, 7, 4, 8, 7, 8, 6, 8, 4, 9, 6, 9, 5,
						9, 3, 10, 0, 10, 2, 10, 2, 11, 0, 11, 1, 11 };
		const int index_count = sizeof(vertex_and_normal_index_array)
				/ sizeof(short);
		cube_mesh_pos_and_norm_index_array.reserve(index_count);
		cube_mesh_indexes.reserve(index_count / 2);
		for (int i = 0; i < index_count; ++i) {
			short value = vertex_and_normal_index_array[i];
			cube_mesh_pos_and_norm_index_array.push_back(value);
			if (i % 2 == 0) {
				cube_mesh_indexes.push_back(value);
			}
		}
	}
	virtual void Update(float deltaTime) {
		theta[axis] += 2.0;
		if (theta[axis] >= 360.0) {
			theta[axis] -= 360.0;
		}
	}
	virtual void OnEvent(const sf::Event& event) {
		switch (event.Type) {
		case sf::Event::MouseButtonPressed:
			if (event.MouseButton.Button == sf::Mouse::Left) {
				axis = 0;
			} else {
				axis = 2;
			}
			break;
		default:
			break;
		}
	}
	virtual void Render() const {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		glRotatef(theta[0], 1.f, 0.f, 0.f);
		glRotatef(theta[1], 0.f, 1.f, 0.f);
		glRotatef(theta[2], 0.f, 0.f, 1.f);

//		// draw mesh
//		glEnableClientState(GL_VERTEX_ARRAY);
//		glVertexPointer(3, GL_FLOAT, 0, &cube_mesh_positions_array[0]);
//		const int triangles_count = cube_mesh_positions_array.size() / 3;
//		glDrawArrays(GL_POINTS, 0, triangles_count);

		glEnableClientState(GL_VERTEX_ARRAY);
		glVertexPointer(3, GL_FLOAT, 0, &cube_mesh_positions_array[0]);
		const int index_count = cube_mesh_indexes.size();
		glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_SHORT,
				&cube_mesh_indexes[0]);
	}

	virtual ~RotateCube() {
	}
};

#endif /* ROTATE_CUBE_H_ */
